package com.esun.payment_api.controller;

import com.esun.common.constant.Constants;
import com.esun.common.utils.AESUtils;
import com.esun.common.utils.DESUtils;
import com.esun.common.utils.SecurityUtils;
import com.esun.payment_api.security.AjaxResult;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
@RequestMapping("/tools")
@CrossOrigin
public class ToolsController {

    @GetMapping("/encodePasswordWithDecodeAES")
    public AjaxResult encodePasswordWithDecodeAES(String password){
        try {
            // 前端AES加密后的密码还原
            String decrypt = AESUtils.decrypt(password);

            HashMap<String, Object> map = new HashMap<>();
            // 采用Security的加密算法再加密
            map.put("encodePwd", SecurityUtils.encryptPassword(decrypt));
            return AjaxResult.success("success", map);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @GetMapping("/encodePassword")
    public AjaxResult encodePassword(String password){
        try {
            // 原始密码，不再用AES解密
            HashMap<String, Object> map = new HashMap<>();
            // 采用Security的加密算法再加密
            map.put("encodePwd", SecurityUtils.encryptPassword(password));
            return AjaxResult.success("success", map);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
